import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
export const store = new Vuex.Store({
  // state 状态
  state: {
    token: localStorage.getItem('token') ? localStorage.getItem('token') : '',
    userAcco: '',
    userId:'',
    userName:'',
  },
  // 进行state操作
  mutations: {
    setToken(state, token) {
      state.token = token;
    },
    removeStorage(state, value) {  // 删除token
      localStorage.removeItem('token');
    },
    setUserAcco(state,acco){
      state.userAcco = acco ; 
    },
    setUserId(state,id){
      state.userId = id;
    },
    setUserName(state,name){
      state.userName = name;
    },
  },
  // 类似vue的计算属性
  getters: {
    token: (state) => state.token,
    getStorage(state,key) {   // 获取本地存储的登录信息
      if (!state.token) {
        state.token = JSON.parse(localStorage.getItem(key))
      }
      return state.token
    },
    userAcco:(state) => state.userAcco,
    userId:(state) => state.userId,
    userName:(state) => state.userName,
  },
  // 多个mutation操作
  action: {

  },
});
export default store;